﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;

namespace Lmondeil.Crm.EF
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                test();
            }
            catch (DbEntityValidationException excValidation)
            {
                foreach (DbEntityValidationResult valResult in excValidation.EntityValidationErrors)
                {
                    foreach (DbValidationError valErr in valResult.ValidationErrors)
                    {
                        Console.WriteLine(valErr.PropertyName + " : " + valErr.ErrorMessage);
                    }
                }
            }

            Console.ReadKey();
        }

        static void test()
        {
            using (CrmContext context = new CrmContext())
            {
                context.Database.Delete();
                context.Database.Create();
                bool compatible = context.Database.CompatibleWithModel(false);
                if (compatible)
                {
                    context.Database.Initialize(true);
                }
            }

            using (CrmContext context = new CrmContext())
            {
                Societe societe = context.Societes.Where(s => s.Nom == "Atlanchim").FirstOrDefault();
                if (societe == null)
                {
                    societe = new Societe()
                    {
                        Nom = "Atlanchim",
                        Description = "Société de production de molécules"
                    };
                }

                context.Societes.Add(societe);
                context.SaveChanges();
            }

            using (CrmContext context = new CrmContext())
            {
                Societe societe = context.Societes.Where(s => s.Nom == "Atlanchim").FirstOrDefault();
                if (societe != null)
                {
                    societe.Adresses.Add(new Adresse()
                    {
                        Ligne1 = "10, route des atomes",
                        CodePostal = 99000,
                        Ville = "Proton sur Mer"
                    });
                }

                context.SaveChanges();
            }

            using (CrmContext context = new CrmContext())
            {
                Societe societe = context.Societes.Where(s => s.Nom == "Atlanchim").FirstOrDefault();
                if (societe != null)
                {
                    Contact contact = new Contact()
                    {
                        Prenom = "Marie-Amélie",
                        Nom = "NICOLAS",
                        Sexe = "F",
                        Langue = "FR"
                    };
                    contact.Societe = societe;
                }

                context.SaveChanges();
            }
        }
    }
}
